package com.chinapost.util;

import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang.StringUtils;

public class DoubleUtil {
	public static Double Keep2(Double d){
		BigDecimal bg = new BigDecimal(d);
		return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	public static Map<String, Double> beanToMaps(Object obj) {
		Map<String, Double> params = new HashMap<String, Double>(0);
		try {
			PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
			PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
			for (int i = 0; i < descriptors.length; i++) {
				String name = descriptors[i].getName();
				if (!StringUtils.equals(name, "class")) {
					Object o = propertyUtilsBean.getNestedProperty(obj, name);
					if (o instanceof Double) {
						Double d = (Double)o;
						if (d>0.0){
							params.put(name, d);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return params;
	}
}
